﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Infragistics.WebUI.UltraWebGrid;
using Infragistics.WebUI.WebCombo;

public partial class page_RequestGood_Edit : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        this.RegisterJS();
                
        if (!IsPostBack)
        {
            lbSourceShopName.Text = _Customer.ShopName;     //获取要货店铺名称


            this.InitComponents();                      //初始化组件
            this.ReadAction();                          //保存传过来的值         
            this.SetPageNoCache();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        //RequestCompetences = Competences.BuildCompetencesList(Competences.DepotIN_Manager);//页面权限

        base.OnLoad(e);
    }

    #endregion

    protected void btnSaveMaster_Click(object sender, EventArgs e)
    {
        trDetail1.Visible = true;
        trDetail2.Visible = true;

        long CreateOperID = _Customer.ID;               //制单人
        string Remark = tbRemark.Text;

        if (hAction.Value == "Edit")                    //更新
        {       
            long ID = Shove._Convert.StrToLong(hID.Value, 0);

            DAL.Tables.tbl_Shop_RequestGoodMaster dtMaster = new DAL.Tables.tbl_Shop_RequestGoodMaster();
            dtMaster.Remark.Value = Remark;

            long i = dtMaster.Update("[ID]=" + ID);

            if (i >= 0)
            {
                Response.Write("<script language='javascript'>alert('数据保存成功！');self.location='RequestGood_Manager.aspx';</script>");
                Response.Flush();
            }
        }
        else                                            //添加
        {
            long tempID =0;
            DAL.Procedures.P_Shop_AddRequestGood_Master(_Customer.ShopID,1,_Site.ID, CreateOperID, Remark, ref tempID); 
            this.hID.Value = tempID.ToString();

            hAction.Value = "Edit";                     //修改状态为编缉
        }   
    }

    protected void btnCancelMaster_Click(object sender, EventArgs e)
    {
        Response.Redirect("RequestGood_Manager.aspx");
    }

    protected void UWGDetail_UpdateRow(object sender, RowEventArgs e)
    {
        if (e.Row.Cells.FromKey("GoodNo").Value != null)
        {
            long DetailID = 0;
            DAL.Tables.tbl_Shop_RequestGoodDetail details = new DAL.Tables.tbl_Shop_RequestGoodDetail();        

            try
            {
                DetailID = Shove._Convert.StrToLong(e.Row.Cells.FromKey("ShopID").ToString(), 0);         //采购入库单明细表ID
            }
            catch { }

            try
            {
                details.Amount.Value = Shove._Convert.StrToInt(e.Row.Cells.FromKey("Amount").ToString(), 0);                 //数量
                details.Price_Orig.Value = Shove._Convert.StrToDouble(e.Row.Cells.FromKey("Price_Orig").ToString(), 0);      //订单价格
                details.ActualPrice.Value = details.Price_Orig.Value;                                                        //实际价格
            }
            catch { }

            if (DetailID == 0)      //表示新增
            {
                details.GoodID.Value = e.Row.Cells.FromKey("GoodID").Value;
                details.RequestGoodID.Value = hID.Value;

                if ((Shove._Convert.StrToInt(details.Amount.Value.ToString(),0)) != 0)
                {

                    int i = DAL.Procedures.P_Shop_AddRequestGood_Detail(Shove._Convert.StrToLong(details.RequestGoodID.Value.ToString(), 0), 1, Shove._Convert.StrToInt(details.Amount.Value.ToString(), 0), Shove._Convert.StrToDouble(details.ActualPrice.Value.ToString(), 0));

                    if (i >= 0)
                    {
                        BindDataDetail();                  
                        BindData();
                    }
                }
                else
                {
                    Shove._Web.JavaScript.Alert(this.Page, "数量为空，添加失败！");
                }
            }
            else                    //表示修改
            {
                details.GoodID.Value = e.Row.Cells.FromKey("ID").Value;
                details.RequestGoodID.Value = this.hID.Value;

                int i = DAL.Procedures.P_Shop_EditRequestGood_Detail(DetailID, Shove._Convert.StrToLong(details.RequestGoodID.Value.ToString(), 0), Shove._Convert.StrToLong(details.GoodID.Value.ToString(), 0), Shove._Convert.StrToInt(details.Amount.Value.ToString(), 0));

                if (i >= 0)
                {
                    BindDataDetail();
                    BindData();
                }
            }            
        }
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        long ID = Shove._Convert.StrToLong(this.hDetail.Value,0);
        int i = DAL.Procedures.P_Shop_DelRequestGood_Detail(ID); ;

        if (i >= 0)
        {
            this.BindDataDetail();               //梆定所选商品

            this.hDetail.Value = "";
        }
    }

    protected void btnMimeograph_Click(object sender, EventArgs e)
    {       
        string ID = hID.Value;

        Response.Write("<script language='javascript'>window.open('RequestGood_Print.aspx?ID=" + ID + "')</script>");
    }

    protected void btnExporterExcel_Click(object sender, EventArgs e)
    {
        Utility.DataTableToExcel.DataTableToExcelAndDownload(dt, "RequestGood_Edit.xls");
    }

    /// <summary>
    /// 注册JS
    /// </summary>
    private void RegisterJS()
    {
        //AjaxPro.Utility.RegisterTypeForAjax(typeof(Ajax.Good));  //注册Ajax方法

        Infragistics.WebUI.Shared.Util.ClientScript.ResolveAndRegisterClientScriptInclude(
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "",
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "ig_webgrid_an.js", this.UWGDetail, "ig_webgrid_an");          //注册 Infragistics js和控件  
    }

    /// <summary>
    /// 初始化组件
    /// </summary>
    private void InitComponents()
    {
        trOrderInfo.Visible = false;
        trDetail1.Visible = false;
        trDetail2.Visible = false;

        Utility.InitUltraWebGrid.InitDisplayLayOut_Oper(this.UWGDetail);
    }

    /// <summary>
    /// 保存传过来的参数
    /// </summary>
    private void ReadAction()
    {
        String sAction = Shove._Web.Utility.GetRequest("Action");
        this.hAction.Value = sAction;

        String sID = Shove._Web.Utility.GetRequest("ID");
        this.hID.Value = sID;

        if (this.hAction.Value == "Edit")
        {
            trOrderInfo.Visible = true;  
            trDetail1.Visible = true;
            trDetail2.Visible = true;

            BindData();
            BindDataDetail();
        }
    }

    /// <summary>
    /// 加载该订单下的所有商品信息
    /// </summary>
    private void BindDataDetail()
    {
        string sID = hID.Value; //订单ID

        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }

        dt = new DAL.Tables.tbl_Shop_RequestGoodDetail().Open("", "RequestGoodID="+sID, ""); 

        UWGDetail.DataSource = dt;
        UWGDetail.DataBind();
    }

    /// <summary>
    /// 初始化订单表头信息
    /// </summary>
    private void BindData()
    {
        string sID = hID.Value; //订单ID
        DataTable dt = new DAL.Views.V_Shop_RequestGoodMaster().Open("","ID="+sID,"");

        if (dt != null&& dt.Rows.Count > 0)
        {
            lbNO.Text = dt.Rows[0]["RequestGoodNO"].ToString();
            lbMoney.Text = dt.Rows[0]["ActualMoney"].ToString();
            tbRemark.Text = dt.Rows[0]["Remark"].ToString();
            lbSourceShopName.Text = dt.Rows[0]["SourceShopName"].ToString();
            dt = null;
        }
    }

    /// </summary>
    /// 设置页面不被缓存
    /// </summary>
    public void SetPageNoCache()
    {
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Expires = 0;
        HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.AddHeader("pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("cache-control", "private");
        HttpContext.Current.Response.CacheControl = "no-cache";
    }
}
